﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperX.TimeManagerService
{
    public sealed class TimerManager
    {
        //维护一个timer list
        private ConcurrentDictionary<string, BaseTimer> timerDic = new ConcurrentDictionary<string, BaseTimer>();

        //Singleton，只能维护一个Manager
        public static readonly TimerManager Instance = new TimerManager();

        private TimerManager()
        { }

        //注册单个BaseTimer
        public void RegiserTimer(string key, BaseTimer baseTimer)
        {
            timerDic.TryAdd(key, baseTimer);
            if (!baseTimer.IsRunning)
            {
                baseTimer.TimerCase.Start();
                baseTimer.IsRunning = true;
            }
        }

        public BaseTimer GetTimer(string key)
        {
            if (!timerDic.ContainsKey(key)) return null;
            return timerDic[key];
        }

        /// <summary>
        /// 注销单个BaseTimer
        /// </summary>
        /// <param name="key"></param>
        public void UnRegiserTimer(string key)
        {
            timerDic.TryRemove(key, out var timer);
        }

        //启动所有Timers
        public void Start()
        {
            if (timerDic.Count() != 0)
            {
                foreach (BaseTimer timer in timerDic.Values)
                {
                    if (!timer.IsRunning)
                    {
                        timer.TimerCase.Start();
                        timer.IsRunning = true;
                    }
                }
            }
        }

        //停止所有Timers
        public void Stop()
        {
            if (timerDic.Count() != 0)
            {
                foreach (BaseTimer timer in timerDic.Values)
                {
                    timer.TimerCase.Stop();
                    timer.TimerCase.Close();
                }
            }
        }

        //根据TimerName停止单个Timer
        public void StopOneTimer(string timerName)
        {
            if (timerDic.Count() != 0)
            {
                var timer = timerDic[timerName];
                if (timer != null)
                {
                    timer.TimerCase.Stop();
                    timer.TimerCase.Close();
                }
            }
        }

        //获取所有Timer状态信息
        public List<TimerStatus> GetStatus()
        {
            List<TimerStatus> statusList = new List<TimerStatus>();
            if (timerDic.Count() != 0)
            {
                foreach (BaseTimer timer in timerDic.Values)
                {
                    statusList.Add(new TimerStatus() { TimerName = timer.TimerName, Enabled = timer.TimerCase.Enabled });
                }
            }

            return statusList;
        }

        //根据TimerName启动单个Timer
        public void StartOneTimer(string timerName)
        {
            if (timerDic.Count() != 0)
            {
                var timer = timerDic[timerName];
                if (timer != null)
                {
                    timer.TimerCase.Start();
                }
            }
        }
    }
}